import {HttpClient} from '@angular/common/http';
import {Injectable} from '@angular/core';

import {Observable} from "rxjs/Observable";
import "rxjs/add/observable/of";

import * as moment from 'moment';

import {DayData, MouthData} from "../../../model/pages/schedule/schedule-list/schedule-list.model";

@Injectable()
export class ScheduleListProvider {


    currentTimestamp: number;


    get nextDay() {
        return this.currentTimestamp + 24 * 60 * 60
    }

    constructor(public http: HttpClient) {
    }


    /**
     * 获取月视图数据
     * @param {number} startTimestamp
     * @param {number} endTimestamp
     * @returns {Observable<any>}
     */
    getMouthData(startTimestamp: number, endTimestamp: number): Observable<MouthData[]> {
        console.log('target');
        let param = ScheduleListProvider.switchToMonth(startTimestamp, endTimestamp);
        return this.http.get(`/reminderNew/v2/reminders/view/${param}`) as Observable<MouthData[]>;
    }

    getDayData(currentTimestamp: number): Observable<DayData[]> {
        this.currentTimestamp = currentTimestamp;
        return this.http.get(`/reminderNew/v2/reminders/${currentTimestamp}`) as Observable<DayData[]>
        // return Observable.of(this.daydata) as Observable<DayData[]>
    }

    deleteSchedule(id: string,appointDay,updateRule): Observable<any> {
        //return this.http.delete(`/reminderNew/v2/${id}`, {responseType: 'text'})
        console.log('delete')
        return this.http.delete('/reminderNew/v2'+'/'+id+'/'+appointDay+'/'+updateRule,{responseType:'text'});
    }

    static switchToMonth(startTimestamp: number, endTimestamp: number): number {
        const step = 24 * 60 * 60;
        let days = [];
        while (startTimestamp !== endTimestamp) {
            days.push(startTimestamp);
            startTimestamp += step
        }
        days.push(endTimestamp);
        let year = moment(new Date(days[7]*1000)).year();
        let month = moment(new Date(days[7]*1000)).month();
        let monthStart=moment([year,month]).unix();
        console.log(monthStart);

        return monthStart;
    }

    /**
     * 把全天的放前面
     * @param res
     */
    parseDayData(res) {
        let allDay = res.filter((v) => {
            return this.isAllDay(v)
        });

        let notAllDay = res.filter((v) => {
            return !this.isAllDay(v)
        });
        return allDay.concat(notAllDay)

    }

    isCurrentDay(item): boolean {
        //开始时间选中日，结束时间是选中日<(正常表示)
        /*if(item.startTime >= this.currentTimestamp && item.endTime < this.nextDay){
            console.log('1')
            console.log(this.currentTimestamp);
        }*/
        return (item.startTime >= this.currentTimestamp && item.endTime < this.nextDay)||item.startTime==item.endTime;
    }

    isAllDay(item): boolean {
        //开始时间选中日之前<=，结束时间是选中日之后>=（表示为全天）

        /*if(item.startTime <= this.currentTimestamp && item.endTime >= this.nextDay){
            console.log('2');
        }*/
        return item.startTime <= this.currentTimestamp && item.endTime >= this.nextDay
    }

    isStartDay(item): boolean {
        //开始时间今天>=，结束时间是今天之后>(显示为开始)(排除全天)
        /*if(item.startTime >= this.currentTimestamp && item.endTime > this.nextDay && !this.isAllDay(item)){
            console.log('3');
        }*/
        return item.startTime >= this.currentTimestamp && item.endTime >= this.nextDay && !this.isAllDay(item);
    }

    isEndDay(item): boolean {
        //开始时间选中日之前<=，结束时间是选中日<,开始时间不等于结束时间(显示为结束)
        /*if(item.startTime < this.currentTimestamp && item.endTime < this.nextDay&&item.startTime!=item.endTime){
            console.log('4');
        }*/
        return item.startTime < this.currentTimestamp && item.endTime < this.nextDay&&item.startTime!=item.endTime;
    }


}
